﻿#include "registerwidget.h"
#include "ui_registerwidget.h"

#include "users.h"
#include <QFile>
#include <QDataStream>
#include <QIODevice>
#include <QMessageBox>
#include <QDebug>
#include <QVector>

RegisterWidget::RegisterWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("注册界面");
}

RegisterWidget::~RegisterWidget()
{
    delete ui;
}

void RegisterWidget::on_btn_back_clicked()
{
    clearAllEditLine();
    this->hide();
    emit goLoginWidget();
}

void RegisterWidget::on_btn_register_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();
    QString confirmPassword = ui->lineEdit_confirmpassword->text();

    if (password != confirmPassword) {
        QMessageBox::critical(this, "错误提示","密码不一致，请重新输入。");
        return;
    }

    IO io;
    QVector<User> v(io.IO::getFileUsernamePassword());
    for(int i = 0; i < v.size(); i++)
    {
        if(username == v.at(i).username)
        {
            QMessageBox::critical(this, "错误提示","已存在该用户，请重新输入。");
            return;
        }
    }
    io.IO::setFileUsernamePassword(username, password);
    QMessageBox::information(this, "成功提示","注册成功");

}

void RegisterWidget::clearAllEditLine()
{
    ui->lineEdit_username->clear();
    ui->lineEdit_password->clear();
    ui->lineEdit_confirmpassword->clear();
}
